package com.smartjobs.action;

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import com.opensymphony.xwork2.ActionSupport;
import com.smartjobs.model.CreateProfileModel;

@SuppressWarnings("serial")
public class SignupAction extends ActionSupport {

	String name;
	String password;
	String emailID;
	String mobileNumber;
	String dob;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmailID() {
		return emailID;
	}

	public void setEmailID(String emailID) {
		this.emailID = emailID;
	}

	public String getMobileNumber() {
		return mobileNumber;
	}

	public void setMobileNumber(String mobileNumber) {
		this.mobileNumber = mobileNumber;
	}

	public String getDob() {
		return dob;
	}

	public void setDob(String dob) {
		this.dob = dob;
	}

	public String execute() {
		return SUCCESS;

	}

	public String createProfile() {
		CreateProfileModel profile = new CreateProfileModel();

		if (name.equals("")) {
			addActionError("Please enter your name.");
			return ERROR;
		} else {
			profile.setName(name);
		}

		if (password.equals("")) {
			addActionError("Please enter your password.");
			return ERROR;
		} else {
			profile.setPassword(password);
		}

		if (emailID.equals("")) {
			addActionError("Please enter your email address.");
			return ERROR;
		} else {
			profile.setEmailID(emailID);
		}

		if (mobileNumber.equals("")) {
			addActionError("Please enter your mobile number.");
			return ERROR;
		} else {
			int mNo = Integer.parseInt(mobileNumber);
			profile.setMobileNumber(mNo);
		}

		if (dob.equals("")) {
			addActionError("Please enter your date of birth.");
			return ERROR;
		} else {
			SimpleDateFormat format1 = new SimpleDateFormat("dd-MM-yyyy");
			SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
			String dateOfBirth = "";
			// Date date = null;
			try {
				Date date = format1.parse(dob);
				dateOfBirth = format2.format(date);
				profile.setDob(dateOfBirth);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		if (profile.insertIntoDB(profile))
			return SUCCESS;
		else
			return ERROR;

	}

}
